import {Item} from "./Type"

let itemData: Array<Item> = []

let itemMap = new Map<number, Item>()

let itemIdNameMap = new Map<number, string>()

export let loadItemData = (): Array<Item> => {
    if (itemData.length > 0) {
        return [...itemData]
    }
    for (let i of defaultData) {
        itemData.push(Object.assign(new Item(), i))
    }

    return [...itemData]
}

export let loadItemMap = (): Map<number, Item> => {
    if (itemMap.size > 0) {
        return new Map(itemMap)
    }
    for (let item of loadItemData()) {
        itemIdNameMap.set(item.id, item.name)
    }
    return new Map(itemMap)
}

export let loadItemIdNameMap = (): Map<number, string> => {
    if (itemIdNameMap.size > 0) {
        return new Map(itemIdNameMap)
    }
    for (let item of loadItemData()) {
        itemIdNameMap.set(item.id, item.name)
    }
    return new Map(itemIdNameMap)
}

let defaultData: Array<any> = [
    {
        "collect": true,
        "handCollect": true,
        "id": 100001,
        "name": "铁矿"
    },
    {
        "collect": true,
        "handCollect": true,
        "id": 100002,
        "name": "铜矿"
    }
]